package com.wistech.search.solr.bench.expression;

import java.util.concurrent.CyclicBarrier;

import org.junit.Before;
import org.junit.Test;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import com.wistech.utils.ExpressionUtils;

public class AviatorBenchmark extends BaseBenchmark {
	private final static String expressString = "level*300+score*50+lastupdate*4+credit*3+completenessRate*2+clickCount+scatter*4";
	private Expression expression;
	private DefaultValue defaultValue;

	@Before
	public void setUp() {
		this.expression = AviatorEvaluator.compile(expressString, true);
		this.defaultValue = new DefaultValue(
				ExpressionUtils.getVars(expressString));
	}

	@Test
	public void test() {
		for (int i = 0; i < THREADS.length; i++) {
			int threads = THREADS[i];
			CyclicBarrier barrier = new CyclicBarrier(threads + 1);
			int repeats = 10;
			int countMax = 100000;
			for (int thread = 0; thread < threads; thread++) {
				new AviatorBenchmarkThread(defaultValue, thread, expression,
						barrier, repeats, countMax).start();
			}
			try {
				barrier.await();
				long start = System.nanoTime();
				barrier.await();
				long duration = System.nanoTime() - start;
				printResult(threads, repeats, countMax, duration);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
